package org.yanghaokun.user.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.yanghaokun.basic.exception.BusinessException;
import org.yanghaokun.basic.service.impl.BaseServiceImpl;
import org.yanghaokun.basic.util.MD5Utils;
import org.yanghaokun.basic.util.StrUtils;
import org.yanghaokun.user.constant.UserConstant;
import org.yanghaokun.user.domain.LoginInfo;
import org.yanghaokun.user.domain.User;
import org.yanghaokun.user.dto.RegisterDto;
import org.yanghaokun.user.mapper.LoginInfoMapper;
import org.yanghaokun.user.mapper.UserMapper;
import org.yanghaokun.user.service.IUserService;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService{
    @Autowired
    private UserMapper mapper;
    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;
    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Transactional
    @Override
    public void phoneRegister(RegisterDto registerDto) {
        //空校验
        if (StringUtils.isEmpty(registerDto.getPhone())||StringUtils.isEmpty(registerDto.getVerifyCode())
                ||StringUtils.isEmpty(registerDto.getPassword())||StringUtils.isEmpty(registerDto.getConfirmPwd())) {
            throw new BusinessException("不能有空白项");
        }
        //看看输入的两次密码是否一样
        if (!registerDto.getPassword().equals(registerDto.getConfirmPwd())) {
            throw new BusinessException("两次密码不一致");
        }
        //看手机号是否被注册
        User user = mapper.findByphone(registerDto.getPhone());
        if (user != null) {
            throw new BusinessException("此账号已被注册");
        }
        //先校验安证码是否有效
        Object codeTmp = redisTemplate.opsForValue().get(UserConstant.REDIS_PREFIX + registerDto.getPhone());
        if (codeTmp == null) {
            throw new BusinessException("验证码已失效，请重新发送");
        }
        //没有失效校验对不对,然后我们直接取出验证码（REDIS_PREFIX【code】：time）REDIS_PREFIX【code】
        String code = ((String) codeTmp).split(":")[0];
        //再校验验证码是否正确
        if (!registerDto.getVerifyCode().equalsIgnoreCase(code)) {
            throw new BusinessException("验证码不正确，请重新输入");
        }
        //注册
        User userTranslate = loginDto2User(registerDto);
        LoginInfo loginInfoTranslate = user2LoginInfo(userTranslate);
        //保存登陆信息
        loginInfoMapper.save(loginInfoTranslate);
        userTranslate.setLoginInfo(loginInfoTranslate);
        //保存用户
        mapper.save(userTranslate);
    }

    @Override
    public User findByphone(String phone) {
        return mapper.findByphone(phone);
    }

    @Override
    public User findOneByLoginInfoId(Long id) {
        return mapper.findByloginId(id);
    }


    /**
     * 声明返回信息
     * 进行转换
     * 返回
     * @param userTranslate
     * @return
     */
    private LoginInfo user2LoginInfo(User userTranslate) {
        LoginInfo loginInfo = new LoginInfo();
        BeanUtils.copyProperties(userTranslate,loginInfo);
        return loginInfo;
    }

    /**
     *  声明返回信息
     * 进行转换
     * 返回
     * @param registerDto
     * @return
     */
    private User loginDto2User(RegisterDto registerDto) {
        //声明对象
        User user = new User();
        //进行转换
        user.setUsername(registerDto.getPhone());
        user.setPhone(registerDto.getPhone());
        //每次输入注册的密码我们都需要随机生成一个盐值
        String salt = StrUtils.getComplexRandomString(32);
        //根据盐值加密密码
        String securityPwd = MD5Utils.pwdByMd5(registerDto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(securityPwd);
        return user;
    }

}
